package com.morehair.config.realm;

import com.morehair.bean.pojo.User;
import com.morehair.bean.pojo.UserExample;
import com.morehair.config.MallToken;
import com.morehair.mapper.UserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Author  kkw
 * Date  2021/6/8 11:17
 * Version 1.0
 * Description :
 */
@Component
public class WxRealm extends AuthorizingRealm {
    @Autowired
    UserMapper userMapper;
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MallToken token= (MallToken) authenticationToken;
        String username = token.getUsername();
//        System.out.println(username);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        User user = users.get(0);
        String credential = user.getPassword();
        System.out.println(credential);
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, credential, this.getName());
        return authenticationInfo;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


}
